﻿/**
 * $RCSfile: WriteTag.java,v $
 * $Revision: 1.1 $
 * $Date: 2009/06/15 08:23:18 $
 *
 * Copyright (C) 2003 ICSS, Inc. All rights reserved.
 *
 * This software is the proprietary information of ICSS, Inc.
 * Use is subject to license terms.
 */
package net.sapfree.display.tags.core;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import net.sapfree.core.db.Record;
import net.sapfree.display.tags.article.ArticleTag;
import net.sapfree.display.tags.article.TagInfoTag;
import net.sapfree.display.tags.person.FriendTag;
import net.sapfree.display.tags.person.JoinedQuanTag;
import net.sapfree.display.tags.person.MyQuanTag;
import net.sapfree.display.tags.person.PersonMessageTag;
import net.sapfree.display.tags.person.PersonTag;
import net.sapfree.display.tags.quan.MemberTag;
import net.sapfree.display.tags.quan.QuanTag;
import net.sapfree.display.tags.quan.VoteTag;
import net.sapfree.display.tags.util.RequestUtils;
import net.sapfree.display.tags.util.ResponseUtils;

import org.apache.log4j.Logger;


 

/**
 * <p>
 * Title: 信息发布系统（CMS）
 * </p>
 * <p>
 * Description: Tag that retrieves the specified property of the specified bean,
 * converts it to a String representation (if necessary), and writes it to the
 * current output stream, optionally filtering characters that are sensitive in
 * HTML.
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2003
 * </p>
 * <p>
 * Company: 中软远东国际
 * </p>
 * 
 * @author Andy
 * @version 1.0
 */
public class WriteTag extends TagSupport {

	// ----------------------------------------------------- Instance Variables

	/**
	 * 
	 */
	private static final long serialVersionUID = 2148752551682307370L;

	/**
	 * Commons Logging instance.
	 */
	protected static Logger log = Logger.getLogger(WriteTag.class.getClass());


	/**
	 * Filter the rendered output for characters that are sensitive in HTML?
	 */
	protected boolean filter = true;

	public boolean getFilter() {
		return this.filter;
	}

	public void setFilter(boolean filter) {
		this.filter = filter;
	}

	/**
	 * Should we ignore missing beans and simply output nothing?
	 */
	protected boolean ignore = false;

	public boolean getIgnore() {
		return this.ignore;
	}

	public void setIgnore(boolean ignore) {
		this.ignore = ignore;
	}

	/**
	 * Name of the bean that contains the data we will be rendering.
	 */
	protected String name = null;

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Name of the property to be accessed on the specified bean.
	 */
	protected String property = null;

	public String getProperty() {
		return this.property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	/**
	 * The scope to be searched to retrieve the specified bean.
	 */
	protected String scope = null;

	public String getScope() {
		return this.scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	/**
	 * The format string to be used as format to convert value to String.
	 */
	protected String formatStr = null;

	public String getFormat() {
		return this.formatStr;
	}

	public void setFormat(String formatStr) {
		this.formatStr = formatStr;
	}

	/**
	 * The key to search format string in applciation resources
	 */
	protected String formatKey = null;

	public String getFormatKey() {
		return this.formatKey;
	}

	public void setFormatKey(String formatKey) {
		this.formatKey = formatKey;
	}

	/**
	 * The session scope key under which our Locale is stored.
	 */
	protected String localeKey = null;

	public String getLocale() {
		return this.localeKey;
	}

	public void setLocale(String localeKey) {
		this.localeKey = localeKey;
	}

	/**
	 * The servlet context attribute key for our resources.
	 */
	protected String bundle = null;

	public String getBundle() {
		return this.bundle;
	}

	public void setBundle(String bundle) {
		this.bundle = bundle;
	}

	// --------------------------------------------------------- Public Methods

	/**
	 * Process the start tag.
	 * 
	 * @exception JspException
	 *                if a JSP exception has occurred
	 */
	public int doStartTag() throws JspException {
		Object value = null;
		if (name != null) {
			value = RequestUtils.lookup(pageContext, name, property, scope);
		} else {
			Record record = null;
			
			Tag parentTag=this.getParent();
			Class a=parentTag.getClass();
			Class[] paramTypes = null;
			Object[] paramValues =null;
			
			
			try {
				Method method = a.getMethod("getRecord", paramTypes);
				record=(Record)method.invoke(parentTag, paramValues);
				  
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}catch (IllegalArgumentException e) {
				e.printStackTrace();
			}catch (Exception e){
				e.printStackTrace();
			}
			/*// PersonTag
			PersonTag person = (PersonTag) pageContext.getAttribute("PersonTag");
			if(person !=null ){
				record = person.getRecord();
			}
			// Step1:  QuanTag
			QuanTag qun = (QuanTag) pageContext.getAttribute("QuanTag");
			if (qun != null) {
				record = qun.getRecord();
			}
			
		
		    
			// MemberTag
			MemberTag member = (MemberTag) pageContext.getAttribute("MemberTag");
			if(member !=null ){
				record = member.getRecord();
			}
			
			// ArticleTag
			ArticleTag article = (ArticleTag) pageContext.getAttribute("ArticleTag");
			if(article !=null ){
				record = article.getRecord();
			}
			
			// TagInfoTag
			TagInfoTag taginfo = (TagInfoTag) pageContext.getAttribute("TagInfoTag");
			if(taginfo !=null ){
				record = taginfo.getRecord();
			}
			
			// VoteTag
			VoteTag vote = (VoteTag) pageContext.getAttribute("VoteTag");
			if(vote !=null ){
				record = vote.getRecord();
			}
			//PersonMessageTag
			PersonMessageTag personmessage = (PersonMessageTag) pageContext.getAttribute("PersonMessageTag");
			if(personmessage !=null){
				record = personmessage.getRecord();
			}
			
			//FriendTag
			FriendTag friend = (FriendTag) pageContext.getAttribute("FriendTag");
			if(friend !=null){
				record = friend.getRecord();
			}
			
			//MyQuanTag
			MyQuanTag myquan = (MyQuanTag) pageContext.getAttribute("MyQuanTag");
			if(myquan != null){
				record = myquan.getRecord();
			}
			
			//JoinedQuanTag
			JoinedQuanTag joinedquan = (JoinedQuanTag) pageContext.getAttribute("JoinedQuanTag");
			if(myquan != null){
				record = joinedquan.getRecord();
			}
			*/
			if(record==null){
				record=new Record();
			}
			value=record.getString(property);
			String output = formatValue(value);
			ResponseUtils.write(pageContext, output);
		}
		
		//value = RequestUtils.lookup(pageContext, name, property, scope);
		
		// Continue processing this page
		return SKIP_BODY;

	}

  

	
	protected String formatValue(Object valueToFormat) throws JspException {
			return valueToFormat.toString();
	}
	
	/**
	 * Release all allocated resources.
	 */
	public void release() {

		super.release();
		filter = true;
		ignore = false;
		name = null;
		property = null;
		scope = null;
		formatStr = null;
		formatKey = null;
		localeKey = null;
		bundle = null;

	}
}